home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / Policy.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.2 KB  |  217 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Policy.java    1.59 98/09/24
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. package java.security;
  17.  
  18. import java.io.*;
  19. import java.lang.RuntimePermission;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;
  22. import java.util.Enumeration;
  23. import java.util.Hashtable;
  24. import java.util.Vector;
  25. import java.util.StringTokenizer;
  26. import java.util.PropertyPermission;
  27.  
  28. import java.lang.reflect.*;
  29.  
  30. /**
  31.  * This is an abstract class for representing the system security
  32.  * policy for a Java application environment (specifying 
  33.  * which permissions are available for code from various sources).
  34.  * That is, the security policy is represented by a Policy subclass
  35.  * providing an implementation of the abstract methods
  36.  * in this Policy class.
  37.  * 
  38.  * <p>There is only one Policy object in effect at any given time.
  39.  * It is consulted by
  40.  * a ProtectionDomain when the protection domain initializes its set of
  41.  * permissions. <p>
  42.  *
  43.  * <p>The source location for the policy information utilized by the
  44.  * Policy object is up to the Policy implementation.
  45.  * The policy configuration may be stored, for example, as a
  46.  * flat ASCII file, as a serialized binary file of
  47.  * the Policy class, or as a database. <p>
  48.  *
  49.  * <p>The currently-installed Policy object can be obtained by 
  50.  * calling the <code>getPolicy</code> method, and it can be 
  51.  * changed by a call to the <code>setPolicy</code> method (by
  52.  * code with permission to reset the Policy).
  53.  * 
  54.  * <p>The <code>refresh</code> method causes the policy
  55.  * object to refresh/reload its current configuration. This is
  56.  * implementation-dependent. For example, if the policy object stores
  57.  * its policy in configuration files, calling <code>refresh</code> will
  58.  * cause it to re-read the configuration policy files.
  59.  * 
  60.  * <p>When a protection domain needs to initialize its set of
  61.  * permissions, it executes code such as the following
  62.  * to ask the currently installed Policy object to populate a
  63.  * PermissionCollection object with the appropriate permissions:
  64.  * <pre>
  65.  *   policy = Policy.getPolicy();
  66.  *   PermissionCollection perms = policy.getPermissions(MyCodeSource)
  67.  * </pre>
  68.  * 
  69.  * <p>The protection domain passes in a CodeSource
  70.  * object, which encapsulates its codebase (URL) and public key attributes.
  71.  * The Policy object evaluates the global policy and 
  72.  * returns an appropriate Permissions object specifying 
  73.  * the permissions allowed for code from the specified code source.
  74.  *
  75.  * <p>The default Policy implementation can be changed by setting the 
  76.  * value of the "policy.provider" security property (in the Java 
  77.  * security properties file) to the fully qualified name of
  78.  * the desired Policy implementation class.
  79.  * The Java security properties file is located in the file named
  80.  * <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME>
  81.  * refers to the directory where the JDK was installed.
  82.  * 
  83.  * @author Roland Schemers
  84.  * @version 1.59, 09/24/98
  85.  * @see java.security.CodeSource
  86.  * @see java.security.PermissionCollection
  87.  */
  88.  
  89. public abstract class Policy {
  90.  
  91.     /** the system-wide policy. */
  92.     private static Policy policy; // package private for AccessControlContext
  93.  
  94.     
  95.     /** package private for AccessControlContext */
  96.     static boolean isSet()
  97.     {
  98.     return policy != null;
  99.     }
  100.  
  101.     /**
  102.      * Returns the installed Policy object. This value should not be cached,
  103.      * as it may be changed by a call to <code>setPolicy</code>. 
  104.      * This method first calls
  105.      * <code>SecurityManager.checkPermission</code> with a
  106.      * <code>SecurityPermission("getPolicy")</code> permission
  107.      * to ensure it's ok to get the Policy object..
  108.      *
  109.      * @return the installed Policy.
  110.      *
  111.      * @throws SecurityException
  112.      *        if a security manager exists and its 
  113.      *        <code>checkPermission</code> method doesn't allow 
  114.      *        getting the Policy object.
  115.      *
  116.      * @see SecurityManager#checkPermission
  117.      */
  118.     public static Policy getPolicy() 
  119.     {
  120.         SecurityManager sm = System.getSecurityManager();
  121.         if (sm != null) sm.checkPermission(new SecurityPermission
  122.                         ("getPolicy"));
  123.     return getPolicyNoCheck();
  124.     }
  125.  
  126.     /**
  127.      * Returns the installed Policy object, skipping the security check.
  128.      * Used by ProtectionDomain and getPolicy.
  129.      *
  130.      * @return the installed Policy.
  131.      *
  132.      */
  133.     static Policy getPolicyNoCheck() 
  134.     {
  135.     if (policy == null) {
  136.  
  137.         synchronized(Policy.class) {
  138.  
  139.         if (policy == null) {
  140.             String policy_class = null;
  141.             policy_class = (String)AccessController.doPrivileged(
  142.                                new PrivilegedAction() {
  143.             public Object run() {
  144.                 return Security.getProperty("policy.provider");
  145.             }
  146.             });
  147.             if (policy_class == null) {
  148.             policy_class = "sun.security.provider.PolicyFile";
  149.             }
  150.  
  151.             try {
  152.             policy = (Policy)
  153.                 Class.forName(policy_class).newInstance();
  154.             } catch (Exception e) {
  155.             policy = new sun.security.provider.PolicyFile();
  156.             }
  157.         }
  158.         }
  159.     }
  160.  
  161.     return policy;
  162.     }
  163.  
  164.     /**
  165.      * Sets the system-wide Policy object. This method first calls 
  166.      * <code>SecurityManager.checkPermission</code> with a
  167.      * <code>SecurityPermission("setPolicy")</code>
  168.      * permission to ensure it's ok to set the Policy.
  169.      *
  170.      * @param policy the new system Policy object.
  171.      *
  172.      * @throws SecurityException
  173.      *        if a security manager exists and its 
  174.      *        <code>checkPermission</code> method doesn't allow 
  175.      *        setting the Policy.
  176.      *
  177.      * @see SecurityManager#checkPermission
  178.      *
  179.      */
  180.     public static void setPolicy(Policy policy)
  181.     {
  182.     SecurityManager sm = System.getSecurityManager();
  183.     if (sm != null) sm.checkPermission(
  184.                  new SecurityPermission("setPolicy"));
  185.     Policy.policy = policy;
  186.     }
  187.  
  188.     /**
  189.      * Evaluates the global policy and returns a
  190.      * PermissionCollection object specifying the set of
  191.      * permissions allowed for code from the specified 
  192.      * code source.
  193.      *
  194.      * @param codesource the CodeSource associated with the caller.
  195.      * This encapsulates the original location of the code (where the code
  196.      * came from) and the public key(s) of its signer.
  197.      *
  198.      * @return the set of permissions allowed for code from <i>codesource</i>
  199.      * according to the policy.  
  200.      *
  201.      * @exception java.lang.SecurityException if the current thread does not 
  202.      *            have permission to call <code>getPermissions</code> on the policy object.
  203.  
  204.      */
  205.     public abstract PermissionCollection getPermissions(CodeSource codesource);
  206.  
  207.     /**
  208.      * Refreshes/reloads the policy configuration. The behavior of this method
  209.      * depends on the implementation. For example, calling <code>refresh</code> 
  210.      * on a file-based policy will cause the file to be re-read.
  211.       *
  212.      * @exception java.lang.SecurityException if the current thread does not 
  213.      *            have permission to refresh this Policy object.
  214.      */
  215.     public abstract void refresh();
  216. }
  217.